﻿
using Dapper;
using MediatR;
using MySql.Data.MySqlClient;
using SCM.API.Read.Application.Command.Ledger;
using SCM.ErrorCode;
using SCM.Extensions.Ledger;
using SCM.Infrastructure.Interface;
using System.Text;

namespace SCM.API.Read.Application.Handler.Ledger
{
    public class GetDistributeHandler : IRequestHandler<GetDistributeCommand, APIPaging<DistributeDTO>>
    {
        IDatabaseContext db;

        public GetDistributeHandler(IDatabaseContext db)
        {
            this.db = db;
        }

        public async Task<APIPaging<DistributeDTO>> Handle(GetDistributeCommand request, CancellationToken cancellationToken)
        {
            var result = new APIPaging<DistributeDTO>();

            var countSql = new StringBuilder();
            var sqlBuilder = new StringBuilder();

            sqlBuilder.Append(@"SELECT 
                                        a.id,
                                        a.Stocknum,
                                        a.Usenum,
                                        a.Waitnum,
                                        a.Exportnum,
                                        d.DepotId,
                                        d.DepotName,
                                        e.LocationId,
                                        e.LocationName,
                                        a.isDel
                                FROM StockDistributeModel a 
                                                  
                                                  join  DepotModel      d on a.DepotId=d.DepotId
                                                  join  LocationModel   e on a.LocationId=e.LocationId
                                                  
                                    WHERE a.IsDel=0   and d.IsDel=0 and e.IsDel=0   ");

            countSql.Append(@"SELECT COUNT(a.id)  FROM StockDistributeModel a 
                                                  
                                                  join  DepotModel      d on a.DepotId=d.DepotId
                                                  join  LocationModel   e on a.LocationId=e.LocationId
                                                  
                                    WHERE a.IsDel=0   and d.IsDel=0 and e.IsDel=0  ");

            var parameters = new DynamicParameters();

            if (!string.IsNullOrEmpty(request.DepotName))
            {
                sqlBuilder.Append(" AND DepotName LIKE @DepotName ");
                parameters.Add("DepotName", $"%{request.DepotName}%");
                countSql.Append(" AND DepotName LIKE @DepotName ");
            }
            if (!string.IsNullOrEmpty(request.LocationName))
            {
                sqlBuilder.Append(" AND LocationName LIKE @LocationName ");
                parameters.Add("LocationName", $"%{request.LocationName}%");
                countSql.Append(" AND LocationName LIKE @LocationName ");
            }
            if (request.DepotId != null)
            {
                sqlBuilder.Append("AND DepotId = @DepotId ");
                parameters.Add("DepotId", request.DepotId);
                countSql.Append("AND DepotId = @DepotId ");
            }
            if (request.LocationId != null)
            {
                sqlBuilder.Append("AND LocationId = @LocationId ");
                parameters.Add("LocationId", request.LocationId);
                countSql.Append("AND LocationId = @LocationId ");
            }


            sqlBuilder.Append("ORDER BY id DESC ");
            sqlBuilder.Append("LIMIT @PageSize OFFSET @Offset ");
            parameters.Add("PageSize", request.PageSize);
            parameters.Add("Offset", (request.PageIndex - 1) * request.PageSize);

           

            var data = await db.DapperConnection.QueryAsync<DistributeDTO>(sqlBuilder.ToString(), parameters);
            var total = await db.DapperConnection.ExecuteScalarAsync<int>(countSql.ToString(), parameters);

            result.Msg = "显示成功";
            result.Code = APIEnum.Success;
            result.items = data.ToList();
            result.totalCount = total;
            result.totalPage = (int)Math.Ceiling((double)total / request.PageSize);
            return result;
        }
    }
}
